/**
 * 
 */
package mdb.com.vn.wservices.utils;

import java.rmi.RemoteException;
import java.util.Date;

import javax.xml.rpc.ServiceException;

import mdb.com.vn.model.Casa;
import mdb.com.vn.ws.t24gold.T24GoldLocator;
import mdb.com.vn.ws.t24gold.T24GoldSoap;
import mdb.com.vn.wservices.T24Locator;
import mdb.com.vn.wservices.T24Soap;

/**
 * @author tu.dod
 * 
 */
public class CasaFromT24 {
	public static Casa getCasaFromT24(Casa casa, String accountLockedNo)
			throws ServiceException, RemoteException {
		T24Soap t24Soap = new T24Locator().getT24Soap();
		//T24GoldSoap t24Soap = new T24GoldLocator().getT24GoldSoap();
		String[][][] fdResult = null;
		String[][] fdResultCus = null;
		String[][][] aciResult = null;
		String strmsg = String
				.format("LIST FBNK.ACCOUNT WITH @ID EQ %s CUSTOMER CATEGORY WORKING.BALANCE CO.CODE INPUTTER ACCT.CREDIT.INT OPENING.DATE",
						accountLockedNo);
		 fdResult = t24Soap.JQL2(strmsg);
		if (fdResult.length != 0) {
			if (fdResult[0][1].length != 0) {
					int count = fdResult[0][6].length;
					if (count != 0) {
						String date = fdResult[0][6][count - 1];
						String newIDACI = fdResult[0][0][0] + "-" + date;
						aciResult = t24Soap
								.JQL2(String
										.format("LIST FBNK.ACCOUNT.CREDIT.INT WITH @ID EQ %s CR.INT.RATE",
												newIDACI));
						if (aciResult.length != 0) {
							casa.setInterestRate(Double.parseDouble(aciResult[0][1][0]
									.equals("") ? "0" : aciResult[0][1][0]));
						}
					}
					casa.setCategory(fdResult[0][2][0]);
					casa.setAccountLocked(accountLockedNo);
					casa.setCustomerId(fdResult[0][1][0]);
					casa.setWbBalances(Double
							.parseDouble(fdResult[0][3].length == 0 ? "0"
									: fdResult[0][3][0]));
					casa.setCreatedBy(fdResult[0][5][0]);
					casa.setBranchCode(fdResult[0][4][0]);
					casa.setOpeningDate(WSUtils.stringToDate(
							fdResult[0][7][0], "yyyyMMdd"));
					casa.setDateCreated(new Date());
					String strmsgcus = String
							.format("LIST FBNK.CUSTOMER WITH @ID EQ %s SHORT.NAME STREET TOWN.COUNTRY SECTOR LEGAL.ID LEGAL.ISS.DATE PROVINCE",
									casa.getCustomerId());
					fdResultCus = t24Soap.JQL1(strmsgcus);
					casa.setCustomerName(fdResultCus[0][1]);
					casa.setAddress(fdResultCus[0][2] + fdResultCus[0][3]);
					casa.setLegalId(fdResultCus[0][5]);
					casa.setLegalIssDate(WSUtils.stringToDate(
							fdResultCus[0][6], "yyyyMMdd"));
					casa.setLegalIssPlace(fdResultCus[0][7]);
					
					//just test
//					casa.setWbBalances(250000000);
//					casa.setCategory("6001");
//					casa.setInterestRate(0.5);
//					casa.setOpeningDate(WSUtils.stringToDate("20131209",
//				"yyyyMMdd"));
					//end

					if (fdResultCus[0][0].substring(0, 1).equals("1")) {
						casa.setCustomerType(1);
					}

				}

		}
		return casa;

	}

}
